// api/user.js
const request = require('./request.js'); // 引入封装好的请求工具

// 查询用户列表
function listUser(query) {
  return request.get('/system/user/list', query);
}

// 查询用户详细
function getUser(userId) {
  return request.get(`/system/user/${userId}`);
}

// 新增用户
function addUser(data) {
  return request.post('/system/user', data);
}

// 修改用户
function updateUser(data) {
  return request.put('/system/user', data);
}

// 删除用户
function delUser(userId) {
  return request.del(`/system/user/${userId}`);
}

// 用户密码重置
function resetUserPwd(userId, password) {
  const data = {
    userId,
    password
  };
  return request.put('/system/user/resetPwd', data);
}

// 用户状态修改
function changeUserStatus(userId, status) {
  const data = {
    userId,
    status
  };
  return request.put('/system/user/changeStatus', data);
}

// 查询用户个人信息
function getUserProfile() {
  return request.get('/system/user/profile');
}

// 修改用户个人信息
function updateUserProfile(data) {
  return request.put('/system/user/profile', data);
}

// 用户密码重置
function updateUserPwd(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword
  };
  return request.put('/system/user/profile/updatePwd', data);
}

// 用户头像上传
function uploadAvatar(data) {
  return request.post('/system/user/profile/avatar', data, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  });
}

// 查询授权角色
function getAuthRole(userId) {
  return request.get(`/system/user/authRole/${userId}`);
}

// 保存授权角色
function updateAuthRole(data) {
  return request.put('/system/user/authRole', data);
}

// 查询部门下拉树结构
function deptTreeSelect() {
  return request.get('/system/user/deptTree');
}

module.exports = {
  listUser,
  getUser,
  addUser,
  updateUser,
  delUser,
  resetUserPwd,
  changeUserStatus,
  getUserProfile,
  updateUserProfile,
  updateUserPwd,
  uploadAvatar,
  getAuthRole,
  updateAuthRole,
  deptTreeSelect
};